package E2020;

import java.io.*;

class Student{
    int stuNo;
    String name;
    String sex;
    int age;
    String grade;
    String classname;

    public Student(int stuNo, String name, String sex, int age, String grade, String classname) {
        this.stuNo = stuNo;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.grade = grade;
        this.classname = classname;
    }
    // 反序列化操作
    public static Student GetStudent(byte[] byteStudent){
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(byteStudent);
            DataInputStream dis = new DataInputStream(bais);
            int stuNO = dis.readInt();
            String name = dis.readUTF();
            String sex = dis.readUTF();
            int age = dis.readInt();
            String grade = dis.readUTF();
            String classname = dis.readUTF();
            return new Student(stuNO,name,sex,age,grade,classname);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    // 序列化操作
    public byte[] GetBytes(){
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeInt(stuNo);
            dos.writeUTF(name);
            dos.writeUTF(sex);
            dos.writeInt(age);
            dos.writeUTF(grade);
            dos.writeUTF(classname);
            return baos.toByteArray();
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
public class Exam2 {
    public static void main(String[] args) {
        Student stu = new Student(20090223,"jack","man",21,"三年级","2班");
        byte[] bytestu = stu.GetBytes();
        Student stu1 = Student.GetStudent(bytestu);
        System.out.println(stu1.stuNo);
    }
}
